#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <time.h>
#include "sem_op.h"

int main()
{
    int sem_id;
    time_t t;
    key_t key = ftok(".", 1);
    if (-1 == key)
    {
        perror("create key\n");
        exit(-1);
    }

    printf("create key success\n");
    sem_id = get_sem(key);
    if (sem_id < 0)
    {
        perror("err create sem\n");
        return 0;
    }
    printf("create sem id success\n");

    while (1)
    {
        sem_p(sem_id, -1);
        t = time(NULL);
        printf("b get sem,%s", ctime(&t));

        sleep(1);
        t = time(NULL);
        printf("again p%s", ctime(&t));
        sem_p(sem_id, -1);

        sleep(1);
        printf("process b p again\n");
    }
    return 0;
}